package co.gov.medellin.ssti.sub.web.action;

import com.cs.beneficios.business.entity.BeneficiarioDAO;
import com.cs.beneficios.business.model.BeneficiarioVO;
import com.cs.beneficios.common.ConnectionFactory;
import com.cs.beneficios.common.util.ControlExceptions;
//import com.cs.beneficios.common.util.ControlExceptions;
import com.cs.paoyer.administration.exception.AdministrationUserException;
import com.cs.paoyer.administration.util.AccessControl;
import com.cs.paoyer.common.exception.CommonUserException;
import com.cs.paoyer.common.exception.UserException;

import java.io.IOException;

import java.sql.SQLException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

public class VerBeneficiosAction extends Action {
	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// ConnectionFactory cf = new
		// ConnectionFactory("beneficios","beneficios");
		ConnectionFactory cf = new ConnectionFactory();
		BeneficiarioVO objBeneVO = new BeneficiarioVO();
		String strCode = "";
		HttpSession session = request.getSession();

		session.removeAttribute("beneficios");
		Collection beneSelected = new ArrayList();
		List beneficiariosList = (List) session.getAttribute("beneficiarios");
		Collection beneficios = null;
		boolean encontro = false;

		ActionMessages messages = new ActionMessages();
		try {
			AccessControl access = new AccessControl();
			access.validateAccess(mapping, request, response);

			strCode = request.getParameter("code");
			if ((Object) strCode == null) {
				List bene = (List) session.getAttribute("beneSelected");
				if (bene != null && bene.size() > 0) {
					BeneficiarioVO objBeneSelectedVO = (BeneficiarioVO) bene
							.get(0);
					strCode = objBeneSelectedVO.getCode().toString();
					request.setAttribute("code", strCode);
				}
			}
			List bene = (List) session.getAttribute("beneSisben");
			if (bene != null) {
				for (int i = 0; i < bene.size(); i++) {
					objBeneVO = (BeneficiarioVO) bene.get(i);
					if (strCode.equals(objBeneVO.getCode().toString())) {
						beneSelected.add(objBeneVO);
						encontro = true;
						session.removeAttribute("beneSisben");
						break;
					}

				}
			}
			if (!encontro) {
				objBeneVO = new BeneficiarioVO();
				objBeneVO.setCode(new Long(strCode));
				try {
					BeneficiarioDAO objBeneDAO = new BeneficiarioDAO(cf);
					beneSelected = objBeneDAO.findBeneficiarioByCriterea(
							objBeneVO, "VerBeneficios");
					bene = (List) beneSelected;
					if (bene != null && bene.size() > 0) {
						objBeneVO = (BeneficiarioVO) bene.get(0);
						encontro = true;
					}

					cf.closeConnection(objBeneDAO.getConn());
				} catch (SQLException e) {
					System.err.println("/******** TDSUB - cwsub");
					e.printStackTrace();
					System.err.println("TDSUB - cwsub ********/");
					ControlExceptions.insertException(e, cf);
				}
			}
			if (!encontro) {
				for (int i = 0; i < beneficiariosList.size(); i++) {
					objBeneVO = (BeneficiarioVO) beneficiariosList.get(i);
					if (strCode.equals(objBeneVO.getCode().toString())) {
						beneSelected.add(objBeneVO);
						encontro = true;
						break;
					}

				}
			}

			try {
				BeneficiarioDAO objBeneDAO = new BeneficiarioDAO(cf);
				beneficios = objBeneDAO.getLastBeneficios(objBeneVO);
				cf.closeConnection(objBeneDAO.getConn());
				session.setAttribute("beneSelected", beneSelected);
				if (beneficios.size() > 0) {
					session.setAttribute("beneficios", beneficios);
				}
			} catch (SQLException e) {
				System.err.println("/******** TDSUB - cwsub");
				e.printStackTrace();
				System.err.println("TDSUB - cwsub ********/");
				ControlExceptions.insertException(e, cf);
			}

			return mapping.findForward("success");

		} catch (UserException ue) {
			ControlExceptions.insertException(ue, cf);
			messages.add("msgWarn", new ActionMessage("loginForm.message."
					+ ue.getExceptionVO().getId()));
			saveMessages(request, messages);
			if (ue.getExceptionVO().getId() == AdministrationUserException.ENDED_SESSION) {
				response.sendRedirect(request.getContextPath() + "/index.jsp");
			} else if (ue.getExceptionVO().getId() == CommonUserException.NOT_ALLOWED_RESOURCE) {
				response.sendRedirect(request.getContextPath()
						+ "/admin/tile/invalidatedResource.jsp");
			} else {
				return mapping.getInputForward();
			}

		}
		return mapping.findForward("success");
	}
}